using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Thauan_Juliano_World
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        List<WorldObject> WorldObjects = new List<WorldObject>();
        List<Tree> trees = new List<Tree>();

        GraphicsDeviceManager graphics;

        Matrix world;
        Camera camera;

        BasicEffect myEffect;

        Grid grid;
        AEstrela aestrela;

        Texture2D arvore;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            base.Initialize();

            this.world = Matrix.CreateWorld(new Vector3(0,0,0), Vector3.Forward, Vector3.Up);
   
            this.camera = new Camera(this, new Vector3(0,10,0));

            GraphicsDevice.RasterizerState = RasterizerState.CullNone;
            GraphicsDevice.BlendState = BlendState.AlphaBlend;

            myEffect = new BasicEffect(GraphicsDevice);
            Inventory inventory = new Inventory(this.GraphicsDevice, this.myEffect);

            this.grid = new Grid(6, 6, this.WorldObjects);
            this.aestrela = new AEstrela();

            this.trees = new List<Tree>();
            for (int t = 0; t < grid.NUM_obstacles; t++)
            {
                Tree tree = new Tree(trees, new Vector3(0, 0, 0), this.arvore, this.camera);
            }
        }
        protected override void LoadContent()
        {
            this.arvore = Content.Load<Texture2D>(@"Images/arvore");
        }

        protected override void UnloadContent()
        {
           
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            camera.Update();

            foreach (WorldObject wo in WorldObjects)
            {
                wo.Update();
            }

            if ( Keyboard.GetState().IsKeyDown(Keys.I))
            {
                this.grid.Raffle(ref this.aestrela.obstacleList, ref this.trees);
            }

            if (Keyboard.GetState().IsKeyDown(Keys.P))
            {
                this.aestrela.Search(this.grid.Nodes[0, 0], this.grid.Nodes[this.grid.Nodes.GetLength(0) - 1, this.grid.Nodes.GetLength(1) - 1],
                                        ref this.grid);
            }

            base.Update(gameTime);
        }


        protected override void Draw(GameTime gameTime)
        {   
            GraphicsDevice.Clear(Color.CornflowerBlue);

            myEffect.World = this.world;
            myEffect.View = camera.View;
            myEffect.Projection = camera.Projector;

            foreach (WorldObject wo in WorldObjects)
            {
                wo.Draw();
            }
            
            if (this.aestrela.obstacleList.Count != 0) 
            {
                for (int i = 0; i < this.aestrela.obstacleList.Count; i++)
                {
                    Vector3 treePos = this.aestrela.obstacleList[i].position;
                    this.trees[i].SetPosition(new Vector3(treePos.X, treePos.Y + 10, treePos.Z));
                    this.trees[i].Draw();
                }
            }

            myEffect.World = world;

            base.Draw(gameTime);
        }
    }
}
